<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/11
 * Time: 18:07
 */
namespace app\admin\controller;
use think\Db;
class Book extends Base {
    public function index(){
        if(request()->isAjax()){
            $update_start = input('update_start');
            $update_end = input('update_end');
            $id = input('keyword');
            $rb_status = input('rb_status');
            $grade_id = input('grade_id');
            $where = array();
            $where['rb_status'] = array('NEQ',3);
            //日期
            if(isset($update_start) && $update_start != '' && isset($update_end) && $update_end != ''){
                $where['rb_update_time'] = ['between', [$update_start, $update_end]];
            }
            if(isset($update_start) && $update_start != '' && isset($update_end) && $update_end == ''){
                $where['rb_update_time'] = ['>=', $update_start];
            }if(isset($update_start) && $update_start == '' && isset($update_end) && $update_end != ''){
                $where['rb_update_time'] = ['<=', $update_end];
            }
            //ID
            $where['rb_id|rb_name|rb_author'] = ['like', '%' . $id . '%'];
            //状态
            if(isset($rb_status) && $rb_status != ''){
                $where['rb_status'] = $rb_status;
            }
            //使用年级
            if(isset($grade_id) && $grade_id != ''){
                $where['rb_grade_id'] = $grade_id;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('reading_book')->alias('a')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('reading_book')->alias('a')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('rb_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['rb_status']==1){
                   $vo['rb_status_name'] = '上架';
                }elseif ($vo['rb_status']==2){
                    $vo['rb_status_name'] = '下架';
                }
                if($vo['rb_type']==1){
                    $vo['rb_type_name']='否';
                }elseif ($vo['rb_type']==2){
                    $vo['rb_type_name']='是';
                }
                //查询年级
                $vo['grade_name'] = Db::table('up_grade')->where('grade_id',$vo['rb_grade_id'])->value('grade_name');
                //查询关卡数量
                $vo['num'] = Db::table('up_reading_game')->where('rg_rb_id',$vo['rb_id'])->count();
                $rb_id = $vo['rb_id'];
               $count1 = Db::table('up_u_user')
                    ->whereOr(function ($query) use ($rb_id){
                        $query->where('user_wanshan',2)
                              ->where('user_pass_one',1)
                              ->where('user_default_book','like','%,'.$rb_id.',%');
                    })
                    ->whereOr(function ($query){
                        $query->where('user_wanshan',2)
                              ->where('user_pass_one',2);
                    })
                ->count();
                 $vo['member_num'] = $count1;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?book_id')){
            session('book_id',null);
        }
        if(session('?book_id1')){
            session('book_id1',null);
        }
        $grade = Db::table('up_grade')
            ->where('grade_status', 1)
            ->where('grade_del', 1)
            ->order('grade_sort asc')
            ->select();
        $this->assign([
           'grade' => $grade,
        ]);
        return $this->fetch();
    }

    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function couponStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择试题']);
        }
        $where['rb_status'] = array('neq',3);
        $where['rb_id'] = array('IN',$ids);
        $status = Db::table('up_reading_book')->where($where)->column('rb_status');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选数据中含有上架中，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选数据中含有下架中，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_reading_book')->where('rb_id', 'IN', $ids)->setField('rb_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 批量删除书籍
     */
    public function del(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
//        $where['rb_status'] = array('neq',3);
//        $where['rb_id'] = array('IN',$ids);
//        $status = Db::table('up_reading_book')->where($where)->column('rb_status');
//        if(in_array(1, $status)){
//            return json(['code' => 100, 'msg' => '所选数据中含有上架中，请重新选择']);
//        }
        $res = Db::table('up_reading_book')->where('rb_id', 'IN', $ids)->setField('rb_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     * 批量删除关卡
     */
    public function delGQ(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
//        $where['rb_status'] = array('neq',3);
//        $where['rb_id'] = array('IN',$ids);
//        $status = Db::table('up_reading_book')->where($where)->column('rb_status');
//        if(in_array(1, $status)){
//            return json(['code' => 100, 'msg' => '所选数据中含有上架中，请重新选择']);
//        }
        $res = Db::table('up_reading_game')->where('rg_id', 'IN', $ids)->delete();
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 导入Excel
     * @return mixed
     */
    public function excelIn(){
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
            for ($i=2;$i<=$highestRow;$i++) {
                $a = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//获取A列的值
                $b = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//获取B列的值
                $c = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//获取C列的值
                $d = $objPHPExcel->getActiveSheet ()->getCell ("D" . $i)->getValue ();//获取D列的值
                $e = $objPHPExcel->getActiveSheet ()->getCell ("E" . $i)->getValue ();//获取E列的值
                $f = $objPHPExcel->getActiveSheet ()->getCell ("F" . $i)->getValue ();//获取F列的值
                $g = $objPHPExcel->getActiveSheet ()->getCell ("G" . $i)->getValue ();//获取F列的值
                $h = $objPHPExcel->getActiveSheet ()->getCell ("H" . $i)->getValue ();//获取F列的值
                $i1 = $objPHPExcel->getActiveSheet ()->getCell ("I" . $i)->getValue ();//获取F列的值
                if($a != '' && $b != '' && $c != '' && $d != '' && $e != '' && $f != '' && $g != '' && $h != ''){
                    $ii = '';
                    if($i1!=''){
                        $ii = Db::table('up_book_type')->where('book_type_id',$i1)->value('book_type_name');
                    }
                    //插入
                    $data = [
                        'rb_name'=>trim($a, ' '),
                        'rb_image'=>$b,
                        'rb_author'=>trim($c, ' '),
                        'rb_company'=>trim($d, ' '),
                        'rb_version'=>trim($e, ' '),
                        'rb_grade_id'=>trim($f, ' '),
                        'rb_score'=>trim($g, ' '),
                        'rb_type'=>trim($h, ' '),
                        'rb_add_time'=>$time,
                        'rb_update_time'=>$time,
                        'rb_book_type_name'=>$ii,
                    ];
                    Db::table('up_reading_book')->insertGetId($data);
                }
            }
            echo '/admin/book/index';
        }
        return $this->fetch('book/excelin');
    }

    /***
     * @return mixed|\think\response\Json
     */
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $time = date('Y-m-d H:i:s',time());
            $data = [
                'rb_name'=>input('param.rb_name'),
                'rb_image'=>input('param.img'),
                'rb_author'=>input('param.rb_author'),
                'rb_company'=>input('param.rb_company'),
                'rb_version'=>input('param.rb_version'),
                'rb_grade_id'=>input('param.rb_grade_id'),
                'rb_score'=>input('param.rb_score'),
                'rb_update_time'=>$time,
                'rb_book_type_name' => input('param.rb_book_type_name')
            ];
            $res = Db::table('up_reading_book')->where('rb_id',$id)->update($data);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $id = input('param.id');
        $info = Db::table('up_reading_book')->where('rb_id',$id)->find();
        $info1 = Db::table('up_grade')->select();
        $book_type = Db::table('up_book_type')->field('book_type_id, book_type_name')->select();
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        $this->assign('book_type',$book_type);
        return $this->fetch();
    }
    /***
     * @return mixed|\think\response\Json
     */
    public function essential (){
        $id = input('param.id');
        $info = Db::table('up_reading_book')->where('rb_id',$id)->find();
        $info1 = Db::table('up_grade')->where('grade_id',$info['rb_grade_id'])->find();
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        return $this->fetch();
    }

    /***
     * 开启信息
     */
    public function start(){
        if(request()->isAjax()){
            $id = input('param.id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            
            $count = Db::table('up_u_user')
                ->whereOr(function ($query) use ($id){
                    $query->where('user_wanshan',2)
                        ->where('user_pass_one',1)
                        ->where('user_default_book','like','%,'.$id.',%');
                })
                ->whereOr(function ($query){
                    $query->where('user_wanshan',2)
                        ->where('user_pass_one',2);
                })
                ->count();
            $lists = Db::table('up_u_user')
                ->whereOr(function ($query) use ($id){
                    $query->where('user_wanshan',2)
                        ->where('user_pass_one',1)
                        ->where('user_default_book','like','%,'.$id.',%');
                })
                ->whereOr(function ($query){
                    $query->where('user_wanshan',2)
                        ->where('user_pass_one',2);
                })
                ->page($Nowpage,$limits)
               ->select();
            foreach ($lists as $Key=>&$vo){
                if($vo['user_type']==1){
                    $vo['user_type_a'] = '学生';
                }
                if($vo['user_type']==2){
                    $vo['user_type_a'] = '老师';
                }
                //段位
                $level = Db::table('up_exp_level')
                    ->where('el_min','<=',$vo['user_exp'])
                    ->order('el_level DESC')
                    ->find();
                $vo['level'] = $level;
                $where1['id'] = array('IN',trim($vo['user_honor'],','));
                $name = Db::table('up_honor')->where($where1)->column('name');
                if(count($name)>0){
                    $vo['name'] = implode(',',$name);
                }else{
                    $vo['name'] = '';
                }
                $where2['rbp_user_id'] = $vo['user_id'];
                $where2['rbp_book_id'] = $id;
                $info = Db::table('up_reading_book_pass')->where($where2)->find();
                if($info['rbp_id']!=''){
                    $vo['is_tg'] = '是';
                }else{
                    $vo['is_tg'] = '否';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        $id = input('id');
        session('book_id',$id);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 闯关信息
     */
    public function checkpoint(){
        if(request()->isAjax()){
            $id = input('param.id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['a.rg_rb_id'] = $id;
            $count = Db::name('reading_game')->alias('a')
                ->join('up_coupon uc','uc.coupon_id = a.rg_coupon','left')
                ->where($where)->count();
            $lists = Db::name('reading_game')->alias('a')
                ->where($where)
                ->join('up_coupon uc','uc.coupon_id = a.rg_coupon','left')
                ->field('a.*,uc.coupon_name, uc.coupon_online')
                ->page($Nowpage,$limits)
                ->order('a.rg_order asc')
                ->select ();
            foreach($lists as $k => $v){
                if($v['coupon_online'] != 1){
                    $lists[$k]['coupon_name'] = '';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('id');
        session('book_id',$id);
        return $this->fetch();
    }

    /**
     * 导入Excel
     * @return mixed
     */
    public function drgq(){
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            for ($i=2;$i<=$highestRow;$i++) {
                $a = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//获取A列的值
                $b = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//获取B列的值
                $c = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//获取C列的值
                $d = $objPHPExcel->getActiveSheet ()->getCell ("D" . $i)->getValue ();//获取D列的值
                $e= $objPHPExcel->getActiveSheet ()->getCell ("E" . $i)->getValue ();//获取D列的值
                if($a != '' && $b != '' && $c != '' && $d != '' && $e != ''){
                    //插入
                    $data = [
                        'rg_name'=>trim($a, ' '),
                        'rg_exp'=>trim($b, ' '),
                        'rg_score'=>trim($c, ' '),
                        'rg_coupon'=>trim($d, ' '),
                        'rg_order'=>trim($e, ' '),
                        'rg_rb_id'=>  session('book_id')
                    ];
                    Db::table('up_reading_game')->insertGetId($data);
                }
            }
            echo '/admin/book/index';
        }
        return $this->fetch('book/drgq');
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑闯关信息
     */
    public function edit(){
        if(request()->isPost()){
            $id = input('param.id');
            $a = input('param.rg_name');
            $b = input('param.rg_exp');
            $c = input('param.rg_score');
            $d = input('param.rb_grade_id');
            $e = input('param.rg_order');
            $data = [
                'rg_name'=>$a,
                'rg_exp'=>$b,
                'rg_score'=>$c,
                'rg_coupon'=>$d,
                'rg_num'=>$e,
            ];
            $res = Db::table('up_reading_game')->where('rg_id',$id)->update($data);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $id = input('param.id');
        $info = Db::table('up_reading_game')->where('rg_id',$id)->find();
        $where1['coupon_type'] = 1;
        $where1['coupon_online'] = 1;
        $where1['coupon_status'] = 1;
        $info1 = Db::table('up_coupon')->where($where1)->select();
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        return $this->fetch();
    }
    public function customs(){
        if(request()->isAjax ()){

            $where = [];
            $where['rs_del'] = 1;
            $id = input('param.id');
            $where['rs_game_id'] = $id;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('reading_subject')->alias('a')
                ->join('up_admin ua','ua.id = a.rs_admin')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('reading_subject')->alias('a')
                ->join('up_admin ua','ua.id = a.rs_admin')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,ua.username')
                ->order('rs_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $rb_name = Db::table('up_reading_book')->where('rb_id',$vo['rs_book_id'])->value('rb_name');
                $rg_name = Db::table('up_reading_game')->where('rg_rb_id',$vo['rs_game_id'])->value('rg_name');
                $vo['rb_name'] = $rb_name;
                $vo['rg_name'] = $rg_name;
                if($vo['rs_type']==1){
                $vo['rs_type_name'] = '认读能力';
                }elseif ($vo['rs_type']==2){
                    $vo['rs_type_name'] = '记诵能力';
                }elseif ($vo['rs_type']==3){
                    $vo['rs_type_name'] = '理解能力';
                }elseif ($vo['rs_type']==4){
                    $vo['rs_type_name'] = '鉴赏能力';
                }elseif ($vo['rs_type']==5){
                    $vo['rs_type_name'] = '活用能力';
                }

            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('id');
        session('book_id1',$id);
        return $this->fetch();
    }
}